<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="80px">
            <el-form-item label="名字" prop="name">
                <el-input v-model="dataForm.name" placeholder="名字"></el-input>
            </el-form-item>
            <el-form-item label="图片地址" prop="pic">
                <el-input v-model="dataForm.pic" placeholder="图片地址"></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
                <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-input v-model="dataForm.status" placeholder="状态"></el-input>
            </el-form-item>
            <el-form-item label="点击数" prop="clickCount">
                <el-input v-model="dataForm.clickCount" placeholder="点击数"></el-input>
            </el-form-item>
            <el-form-item label="广告详情连接地址" prop="url">
                <el-input v-model="dataForm.url" placeholder="广告详情连接地址"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="note">
                <el-input v-model="dataForm.note" placeholder="备注"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
            </el-form-item>
            <el-form-item label="发布者" prop="publisherId">
                <el-input v-model="dataForm.publisherId" placeholder="发布者"></el-input>
            </el-form-item>
            <el-form-item label="审核者" prop="authId">
                <el-input v-model="dataForm.authId" placeholder="审核者"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: 0,
                    name: '',
                    pic: '',
                    startTime: '',
                    endTime: '',
                    status: '',
                    clickCount: '',
                    url: '',
                    note: '',
                    sort: '',
                    publisherId: '',
                    authId: ''
                },
                dataRule: {
                    name: [
                        {required: true, message: '名字不能为空', trigger: 'blur'}
                    ],
                    pic: [
                        {required: true, message: '图片地址不能为空', trigger: 'blur'}
                    ],
                    startTime: [
                        {required: true, message: '开始时间不能为空', trigger: 'blur'}
                    ],
                    endTime: [
                        {required: true, message: '结束时间不能为空', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '状态不能为空', trigger: 'blur'}
                    ],
                    clickCount: [
                        {required: true, message: '点击数不能为空', trigger: 'blur'}
                    ],
                    url: [
                        {required: true, message: '广告详情连接地址不能为空', trigger: 'blur'}
                    ],
                    note: [
                        {required: true, message: '备注不能为空', trigger: 'blur'}
                    ],
                    sort: [
                        {required: true, message: '排序不能为空', trigger: 'blur'}
                    ],
                    publisherId: [
                        {required: true, message: '发布者不能为空', trigger: 'blur'}
                    ],
                    authId: [
                        {required: true, message: '审核者不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        this.$http({
                            url: this.$http.adornUrl(`/coupon/homeadv/info/${this.dataForm.id}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm.name = data.homeAdv.name
                                this.dataForm.pic = data.homeAdv.pic
                                this.dataForm.startTime = data.homeAdv.startTime
                                this.dataForm.endTime = data.homeAdv.endTime
                                this.dataForm.status = data.homeAdv.status
                                this.dataForm.clickCount = data.homeAdv.clickCount
                                this.dataForm.url = data.homeAdv.url
                                this.dataForm.note = data.homeAdv.note
                                this.dataForm.sort = data.homeAdv.sort
                                this.dataForm.publisherId = data.homeAdv.publisherId
                                this.dataForm.authId = data.homeAdv.authId
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/coupon/homeadv/${!this.dataForm.id ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData({
                                'id': this.dataForm.id || undefined,
                                'name': this.dataForm.name,
                                'pic': this.dataForm.pic,
                                'startTime': this.dataForm.startTime,
                                'endTime': this.dataForm.endTime,
                                'status': this.dataForm.status,
                                'clickCount': this.dataForm.clickCount,
                                'url': this.dataForm.url,
                                'note': this.dataForm.note,
                                'sort': this.dataForm.sort,
                                'publisherId': this.dataForm.publisherId,
                                'authId': this.dataForm.authId
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
